class ListNode {
  val: number;
  next: ListNode | null;
  constructor(val?: number, next?: ListNode | null) {
    this.val = val === undefined ? 0 : val;
    this.next = next === undefined ? null : next;
  }
}
function reverseList(head: ListNode | null): ListNode | null {
  // 1. 判断节点为null或者只有一个节点，直接原路返回
  if (!head || !head.next) return head;
  // 2. 反转链表结构
  let newHead = null;

  while (head) {
    let current: ListNode | null = head.next; //用来保持后一个节点的链接
    head.next = newHead; // 解除next指向newHead
    newHead = head; // newHead需要最新的节点表示
    head = current; // 准备下一次节点替换
  }

  return newHead;
}

const head1 = new ListNode(1, new ListNode(2, new ListNode(3)));

console.dir(reverseList(head1));

export {};
